home *** CD-ROM | disk | FTP | other *** search
/ Just Call Me Internet / Just Call Me Internet.iso / com / internet / sting / shttpd20 / demo / cgi-src / passutil / util.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-10-14  |  3.2 KB  |  161 lines

  1. /*
  2.         BibliothŠque d'outils d'extaction de donn‚es
  3. */
  4.  
  5. #include    <stdio.h>
  6. #include    <string.h>
  7. #include    <stdlib.h>
  8.  
  9. #include    "util.h"
  10.  
  11. #define LF 10
  12. #define CR 13
  13.  
  14. void getword(char *word, char *line, char stop) {
  15.     int x = 0,y;
  16.  
  17.     for(x=0;((line[x]) && (line[x] != stop));x++)
  18.         word[x] = line[x];
  19.  
  20.     word[x] = '\0';
  21.     if(line[x]) ++x;
  22.     y=0;
  23.  
  24.     while(line[y++] = line[x++]);
  25. }
  26.  
  27. char *makeword(char *line, char stop) {
  28.     int x = 0,y;
  29.     char *word = (char *) malloc(sizeof(char) * (strlen(line) + 1));
  30.  
  31.     for(x=0;((line[x]) && (line[x] != stop));x++)
  32.         word[x] = line[x];
  33.  
  34.     word[x] = '\0';
  35.     if(line[x]) ++x;
  36.     y=0;
  37.  
  38.     while(line[y++] = line[x++]);
  39.     return word;
  40. }
  41.  
  42. char *fmakeword(FILE *f, char stop, int *cl)
  43. {
  44. /*  int wsize; */
  45. long wsize;
  46. char *word;
  47. int ll;
  48.  
  49.     wsize = 102400L;
  50.     ll=0;
  51.     word = (char *) malloc(sizeof(char) * (wsize + 1));
  52.     
  53.     if ( word == NULL )
  54.         fprintf(stderr,"\aMemory error\n");
  55.  
  56.     while(1) {
  57.         word[ll] = (char)fgetc(f);
  58.         if(ll==wsize) {
  59.             word[ll+1] = '\0';
  60.             wsize+=102400L;
  61.             word = (char *)realloc(word,sizeof(char)*(wsize+1));
  62.         }
  63.         --(*cl);
  64.         if((word[ll] == stop) || (feof(f)) || (!(*cl))) {
  65.             if(word[ll] != stop) ll++;
  66.             word[ll] = '\0';
  67.             return word;
  68.         }
  69.         ++ll;
  70.     }
  71. }
  72.  
  73. char x2c(char *what) {
  74.     register char digit;
  75.  
  76.     digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
  77.     digit *= 16;
  78.     digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
  79.     return(digit);
  80. }
  81.  
  82. void unescape_url(char *url) {
  83.     register int x,y;
  84.  
  85.     for(x=0,y=0;url[y];++x,++y) {
  86.         if((url[x] = url[y]) == '%') {
  87.             url[x] = x2c(&url[y+1]);
  88.             y+=2;
  89.         }
  90.     }
  91.     url[x] = '\0';
  92. }
  93.  
  94. void plustospace(char *str) {
  95.     register int x;
  96.  
  97.     for(x=0;str[x];x++) if(str[x] == '+') str[x] = ' ';
  98. }
  99.  
  100. int rind(char *s, char c) {
  101.     register int x;
  102.     for(x=(int)strlen(s) - 1;x != -1; x--)
  103.         if(s[x] == c) return x;
  104.     return -1;
  105. }
  106.  
  107. int getline(char *s, int n, FILE *f) {
  108.     register int i=0;
  109.  
  110.     while(1) {
  111.         s[i] = (char)fgetc(f);
  112.  
  113.         if(s[i] == CR)
  114.             s[i] = fgetc(f);
  115.  
  116.         if((s[i] == 0x4) || (s[i] == LF) || (i == (n-1))) {
  117.             s[i] = '\0';
  118.             return (feof(f) ? 1 : 0);
  119.         }
  120.         ++i;
  121.     }
  122. }
  123.  
  124. void send_fd(FILE *f, FILE *fd)
  125. {
  126. /*   int num_chars=0; */
  127.     char c;
  128.  
  129.     while (1) {
  130.         c = fgetc(f);
  131.         if(feof(f))
  132.             return;
  133.         fputc(c,fd);
  134.     }
  135. }
  136.  
  137. int ind(char *s, char c) {
  138.     register int x;
  139.  
  140.     for(x=0;s[x];x++)
  141.         if(s[x] == c) return x;
  142.  
  143.     return -1;
  144. }
  145.  
  146. void escape_shell_cmd(char *cmd) {
  147.     register int x,y,l;
  148.  
  149.     l=(int)strlen(cmd);
  150.     for(x=0;cmd[x];x++) {
  151.         if(ind("&;`'\"|*?~<>^()[]{}$\\",cmd[x]) != -1){
  152.             for(y=l+1;y>x;y--)
  153.                 cmd[y] = cmd[y-1];
  154.             l++; /* length has been increased */
  155.             cmd[x] = '\\';
  156.             x++; /* skip the character */
  157.         }
  158.     }
  159. }
  160.  
  161. /* ----- EOF ----- */